package red.cola.common.config.exception;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import red.cola.result.Result;

import java.nio.file.AccessDeniedException;

/**
 * @Name: SysRoleController
 * @Author: Cola
 * @Time: 2023/3/6 11:25
 * @Description: SysRoleController
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理，执行的方法
     *
     * @param e Exception
     * @return Result.fail().message
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("全局异常处理");
    }

    /**
     * 特定异常处理
     *
     * @param e ArithmeticException
     * @return Result.fail().message
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("特定异常处理");
    }

    /**
     * 自定义异常处理
     *
     * @param e ColaException
     * @return Result.fail().message
     */
    @ExceptionHandler(ColaException.class)
    @ResponseBody
    public Result error(ColaException e) {
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMsg());
    }

    /**
     * spring security异常
     *
     * @param e AccessDeniedException
     * @return Result.fail().message
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail().code(205).message("没有操作权限");
    }
}
